<template>
	<view>
		<van-collapse :value="showOrder" accordion @change="onChange">
			<van-collapse-item v-for="(item,index) in list" :key="item.orderId">
				<template #title>
					<view style="font-size: 30rpx; display: flex;align-items:center;justify-content: space-between;">
						<view style="font-weight: 600;" v-text="item.shopName+'>'" @click="onClickShop(item.shopId)"></view>
						<view v-if="item.status==-2" style="color:#808080;">卖家取消</view>
						<view v-else-if="item.status==-1" style="color: #808080;">买家取消</view>
						<view v-else-if="item.status==0" style="color: #808080;">已下单</view>
						<view v-else-if="item.status==1" style="color: #4CD964;">进行中</view>
						<view v-else-if="item.status==2" style="color:#F0AD4E;">已完成</view>
						<view v-text="getTime(item.time)" />
					</view>
				</template>
				<!-- 信息 -->
				<view>
					<view style="display: flex; align-items: center; justify-content: space-between;">
						<view v-text="'订单号'+item.orderId"></view>
						<view style="color: #DD524D;" @click="onDelete(item.orderId)">删除</view>
					</view>
					<van-steps v-if="item.status>=0" :active="item.status" :steps="steps">
						<van-step>买家下单</van-step>
						<van-step>商家接单</van-step>
						<van-step>交易完成</van-step>
					</van-steps>
					<van-cell-group title="菜品信息">
						<view v-for="(iitem,iindex) in item.dish" :key="iitem.dishName">
							<van-cell :title="iitem.dishName" :value="'¥'+(iitem.price.toFixed(2)*iitem.value).toFixed(2)" :label="'×'+iitem.value" />
						</view>
						<van-cell title="总价" :value="'¥'+item.totprice.toFixed(2)" />
					</van-cell-group>
					<van-cell-group title="收货人信息" v-if="item.method==0">
						<van-cell title="姓名" :value="item.name" />
						<van-cell title="电话" :value="item.tel" />
						<van-cell title="姓名" :value="item.address" />
					</van-cell-group>
					<van-cell-group title="其他">
						<van-cell v-if="item.method==0" title="订餐方式" value="外卖" />
						<van-cell v-else title="订餐方式" value="自取" />
					</van-cell-group>
					<view style="display: flex;align-items: center;justify-content: space-around;">
						<button v-if="item.status==0" plain class="button" @click="onClick(0,item.orderId)">取消订单</button>
						<button v-if="item.status>=1" plain class="button" @click="onClick(1,item.orderId)">举报商家</button>
						<button v-if="item.status>=1" plain class="button" @click="onClick(2,item.orderId)">联系商家</button>
						<button v-if="item.status==2" plain class="button" @click="onClick(3,item.orderId)" url="">写个评价</button>
					</view>
					</van-cell-group>
				</view>
			</van-collapse-item>
		</van-collapse>
		<view class="safeView"></view>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				showOrder:-1,
				list:[],
				steps: [
				        {
				          text: '买家下单',
				        },
				        {
				          text: '卖家接单',
				        },
				        {
				          text: '交易完成',
				        }
				    ]
			}
		},
		onLoad(param){
			const that=this;
		},
		onShow(){
			const that=this;
			that.getOrder();
		},
		methods:{
			//get
			getOrder(){
				const that=this;
				wx.showLoading();
				wx.request({
					url:that.myConfig.apiUrl+'/getOrder.php',
					method:'POST',
					data:{
						openId:that.myConfig.openId
					},
					success:function(res){
						wx.hideLoading();
						console.log('getOrder',res);
						that.list=res.data.order;
					},
					fail:function(){
						wx.hideLoading();
						that.myConfig,err();
					}
				});
			},
			//点击店铺
			onClickShop(shopId){
				uni.navigateTo({
					url:'/pages/shop/shop?'+'shopId='+shopId
				});
			},
			//转化事件
			getTime(time){
				const that=this;
				return that.myConfig.tansf(time);
			},
			//切换展示的订单
			onChange(value){
				const that=this;
				if(that.showOrder!=value.detail)
					that.showOrder=value.detail;
				else
					that.showOrder=-1;
			},
			//删除订单
			onDelete(orderId){
				const that=this;
				for(let i=0;i<that.list.length;i++){
					if(that.list[i].orderId==orderId&&(that.list[i].status==1||that.list[i].status==0)){
						uni.showToast({
							title:'订单正在进行',
							icon:'none',
							duration:1200
						});
						return ;
					}
				}
				uni.showModal({
					title:'林大干饭人',
					content:'你确认要删除吗',
					cancelText:'点错了',
					confirmText:'确认删除',
					success:function(mod){
						if(mod.confirm){
							wx.showLoading();
							wx.request({
								url:that.myConfig.apiUrl+'/deleteOrder.php',
								method:'POST',
								data:{
									openId:that.myConfig.openId,
									orderId:orderId
								},
								success:function(res){
									wx.hideLoading();
									console.log('deleteOrder',res);
									uni.showToast({
										title:'删除成功',
										icon:'none',
										duration:1200
									});
									that.showOrder=-1;
									that.getOrder();
								},
								fail:function(){
									wx.hideLoading();
									that.myConfig.err();
								}
							});
						}
					}
				});
			},
			//点击按钮
			onClick(type,orderId){
				const that=this;
				if(type==0){
					uni.showModal({
						title:'林大干饭人',
						content:'你确认要取消订单吗',
						cancelText:'点错了',
						confirmText:'确认取消',
						success:function(mod){
							if(mod.confirm){
								wx.showLoading();
								wx.request({
									url:that.myConfig.apiUrl+'/changeOrderStatus.php',
									method:'POST',
									data:{
										openId:that.myConfig.openId,
										orderId:orderId,
										status:-1
									},
									success:function(res){
										wx.hideLoading();
										console.log('changeOrderStatus',res);
										wx.showToast({
											title:'取消成功',
											icon:'none',
											duration:1200
										});
										that.getOrder();
									},
									fail:function(){
										wx.hideLoading();
										that.myConfig.err();
									}
								});
							}
						}
					});
				}else if(type==1){
					const that=this;
					uni.navigateTo({
						url:'../report/report?orderId='+orderId
					});
				}else if(type==2){
					const that=this;
					let num='';
					for(let i=0;i<that.list.length;i++){
						if(that.list[i].orderId==orderId){
							num=that.list[i].shopTel+'';
							break;
						}
					}
					wx.makePhoneCall({
					  phoneNumber:num  //仅为示例，并非真实的电话号码
					});
				}else{
					const that=this;
					uni.navigateTo({
						url:'../writeReview/writeReview?orderId='+orderId
					});
				}
			}
		}
	}
</script>

<style scoped>
	.button{
		width: 33%;
		color: #969799;
		border: 0rpx;
		font-size: 25rpx;
	}
</style>
